
package com.kanjian.music.fragment;

import org.jivesoftware.smack.packet.Message;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.kanjian.music.R;
import com.kanjian.music.adapter.LiveChatListAdapter;
import com.kanjian.music.service.XMPPService;
import com.kanjian.music.service.XMPPService.XMPPServiceBinder;
import com.kanjian.music.xmpp.OnChatListener;
import com.kanjian.music.xmpp.XMPPManager;

public class ChatFragment extends Fragment implements OnChatListener {

    public static ChatFragment newInstance() {
        ChatFragment f = new ChatFragment();
        Bundle b = new Bundle();
        f.setArguments(b);
        return f;
    }

    private XMPPManager mChatManager;

    private View mRootView;

    private ListView mListView;

    private LiveChatListAdapter mListAdapater;

    private EditText mChatEdit;

    private Button mChatBtn;

    private XMPPService mChatService;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mChatManager = XMPPManager.getIntance();
        getActivity().bindService(new Intent(getActivity(), XMPPService.class), mServiceConnection,
                Context.BIND_AUTO_CREATE);
        mListAdapater = new LiveChatListAdapter(getActivity());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mChatManager.unregisterOnChatListener(this);
        getActivity().unbindService(mServiceConnection);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mRootView == null) {
            initView(inflater, container);
        }
        return mRootView;
    }

    private void initView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_live_chat, container, false);
        mListView = (ListView) mRootView.findViewById(R.id.live_chat_listview);
        mListView.setAdapter(mListAdapater);
        mChatEdit = (EditText) mRootView.findViewById(R.id.live_chat_edit);
        mChatBtn = (Button) mRootView.findViewById(R.id.live_chat_btn);
        mChatBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mChatService.sendMessage(mChatEdit.getText().toString());
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mRootView = null;
    }
    

    @Override
    public void onMessage(final Message msg) {
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mListAdapater != null) {
                    mListAdapater.addMessage(msg.getFrom() + msg.getBody());
                    mListView.setSelection(mListAdapater.getCount());
                }
            }
        });
    }

    ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mChatService = ((XMPPServiceBinder) service).getService();
            mChatManager.registerOnChatListener(ChatFragment.this);
        }
    };

}
